home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 13 / CU Amiga Magazine's Super CD-ROM 13 (1997)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1997-08].iso / CUCD / Graphics / Ghostscript / src / jpeg-6a / jcmaster.c < prev    next >
C/C++ Source or Header  |  1996-01-06  |  19KB  |  579 lines

  1. /*
  2.  * jcmaster.c
  3.  *
  4.  * Copyright (C) 1991-1996, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains master control logic for the JPEG compressor.
  9.  * These routines are concerned with parameter validation, initial setup,
  10.  * and inter-pass control (determining the number of passes and the work 
  11.  * to be done in each pass).
  12.  */
  13.  
  14. #define JPEG_INTERNALS
  15. #include "jinclude.h"
  16. #include "jpeglib.h"
  17.  
  18.  
  19. /* Private state */
  20.  
  21. typedef enum {
  22.     main_pass,        /* input data, also do first output step */
  23.     huff_opt_pass,        /* Huffman code optimization pass */
  24.     output_pass        /* data output pass */
  25. } c_pass_type;
  26.  
  27. typedef struct {
  28.   struct jpeg_comp_master pub;    /* public fields */
  29.  
  30.   c_pass_type pass_type;    /* the type of the current pass */
  31.  
  32.   int pass_number;        /* # of passes completed */
  33.   int total_passes;        /* total # of passes needed */
  34.  
  35.   int scan_number;        /* current index in scan_info[] */
  36. } my_comp_master;
  37.  
  38. typedef my_comp_master * my_master_ptr;
  39.  
  40.  
  41. /*
  42.  * Support routines that do various essential calculations.
  43.  */
  44.  
  45. LOCAL(void)
  46. initial_setup (j_compress_ptr cinfo)
  47. /* Do computations that are needed before master selection phase */
  48. {
  49.   int ci;
  50.   jpeg_component_info *compptr;
  51.   long samplesperrow;
  52.   JDIMENSION jd_samplesperrow;
  53.  
  54.   /* Sanity check on image dimensions */
  55.   if (cinfo->image_height <= 0 || cinfo->image_width <= 0
  56.       || cinfo->num_components <= 0 || cinfo->input_components <= 0)
  57.     ERREXIT(cinfo, JERR_EMPTY_IMAGE);
  58.  
  59.   /* Make sure image isn't bigger than I can handle */
  60.   if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
  61.       (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
  62.     ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
  63.  
  64.   /* Width of an input scanline must be representable as JDIMENSION. */
  65.   samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
  66.   jd_samplesperrow = (JDIMENSION) samplesperrow;
  67.   if ((long) jd_samplesperrow != samplesperrow)
  68.     ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  69.  
  70.   /* For now, precision must match compiled-in value... */
  71.   if (cinfo->data_precision != BITS_IN_JSAMPLE)
  72.     ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
  73.  
  74.   /* Check that number of components won't exceed internal array sizes */
  75.   if (cinfo->num_components > MAX_COMPONENTS)
  76.     ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
  77.          MAX_COMPONENTS);
  78.  
  79.   /* Compute maximum sampling factors; check factor validity */
  80.   cinfo->max_h_samp_factor = 1;
  81.   cinfo->max_v_samp_factor = 1;
  82.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  83.        ci++, compptr++) {
  84.     if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
  85.     compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
  86.       ERREXIT(cinfo, JERR_BAD_SAMPLING);
  87.     cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
  88.                    compptr->h_samp_factor);
  89.     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
  90.                    compptr->v_samp_factor);
  91.   }
  92.  
  93.   /* Compute dimensions of components */
  94.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  95.        ci++, compptr++) {
  96.     /* Fill in the correct component_index value; don't rely on application */
  97.     compptr->component_index = ci;
  98.     /* For compression, we never do DCT scaling. */
  99.     compptr->DCT_scaled_size = DCTSIZE;
  100.     /* Size in DCT blocks */
  101.     compptr->width_in_blocks = (JDIMENSION)
  102.       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  103.             (long) (cinfo->max_h_samp_factor * DCTSIZE));
  104.     compptr->height_in_blocks = (JDIMENSION)
  105.       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  106.             (long) (cinfo->max_v_samp_factor * DCTSIZE));
  107.     /* Size in samples */
  108.     compptr->downsampled_width = (JDIMENSION)
  109.       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  110.             (long) cinfo->max_h_samp_factor);
  111.     compptr->downsampled_height = (JDIMENSION)
  112.       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  113.             (long) cinfo->max_v_samp_factor);
  114.     /* Mark component needed (this flag isn't actually used for compression) */
  115.     compptr->component_needed = TRUE;
  116.   }
  117.  
  118.   /* Compute number of fully interleaved MCU rows (number of times that
  119.    * main controller will call coefficient controller).
  120.    */
  121.   cinfo->total_iMCU_rows = (JDIMENSION)
  122.     jdiv_round_up((long) cinfo->image_height,
  123.           (long) (cinfo->max_v_samp_factor*DCTSIZE));
  124. }
  125.  
  126.  
  127. #ifdef C_MULTISCAN_FILES_SUPPORTED
  128.  
  129. LOCAL(void)
  130. validate_script (j_compress_ptr cinfo)
  131. /* Verify that the scan script in cinfo->scan_info[] is valid; also
  132.  * determine whether it uses progressive JPEG, and set cinfo->progressive_mode.
  133.  */
  134. {
  135.   const jpeg_scan_info * scanptr;
  136.   int scanno, ncomps, ci, coefi, thisi;
  137.   int Ss, Se, Ah, Al;
  138.   boolean component_sent[MAX_COMPONENTS];
  139. #ifdef C_PROGRESSIVE_SUPPORTED
  140.   int * last_bitpos_ptr;
  141.   int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
  142.   /* -1 until that coefficient has been seen; then last Al for it */
  143. #endif
  144.  
  145.   if (cinfo->num_scans <= 0)
  146.     ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);
  147.  
  148.   /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
  149.    * for progressive JPEG, no scan can have this.
  150.    */
  151.   scanptr = cinfo->scan_info;
  152.   if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {
  153. #ifdef C_PROGRESSIVE_SUPPORTED
  154.     cinfo->progressive_mode = TRUE;
  155.     last_bitpos_ptr = & last_bitpos[0][0];
  156.     for (ci = 0; ci < cinfo->num_components; ci++) 
  157.       for (coefi = 0; coefi < DCTSIZE2; coefi++)
  158.     *last_bitpos_ptr++ = -1;
  159. #else
  160.     ERREXIT(cinfo, JERR_NOT_COMPILED);
  161. #endif
  162.   } else {
  163.     cinfo->progressive_mode = FALSE;
  164.     for (ci = 0; ci < cinfo->num_components; ci++) 
  165.       component_sent[ci] = FALSE;
  166.   }
  167.  
  168.   for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
  169.     /* Validate component indexes */
  170.     ncomps = scanptr->comps_in_scan;
  171.     if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
  172.       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
  173.     for (ci = 0; ci < ncomps; ci++) {
  174.       thisi = scanptr->component_index[ci];
  175.       if (thisi < 0 || thisi >= cinfo->num_components)
  176.     ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
  177.       /* Components must appear in SOF order within each scan */
  178.       if (ci > 0 && thisi <= scanptr->component_index[ci-1])
  179.     ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
  180.     }
  181.     /* Validate progression parameters */
  182.     Ss = scanptr->Ss;
  183.     Se = scanptr->Se;
  184.     Ah = scanptr->Ah;
  185.     Al = scanptr->Al;
  186.     if (cinfo->progressive_mode) {
  187. #ifdef C_PROGRESSIVE_SUPPORTED
  188.       if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
  189.       Ah < 0 || Ah > 13 || Al < 0 || Al > 13)
  190.     ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  191.       if (Ss == 0) {
  192.     if (Se != 0)        /* DC and AC together not OK */
  193.       ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  194.       } else {
  195.     if (ncomps != 1)    /* AC scans must be for only one component */
  196.       ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  197.       }
  198.       for (ci = 0; ci < ncomps; ci++) {
  199.     last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];
  200.     if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
  201.       ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  202.     for (coefi = Ss; coefi <= Se; coefi++) {
  203.       if (last_bitpos_ptr[coefi] < 0) {
  204.         /* first scan of this coefficient */
  205.         if (Ah != 0)
  206.           ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  207.       } else {
  208.         /* not first scan */
  209.         if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)
  210.           ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  211.       }
  212.       last_bitpos_ptr[coefi] = Al;
  213.     }
  214.       }
  215. #endif
  216.     } else {
  217.       /* For sequential JPEG, all progression parameters must be these: */
  218.       if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)
  219.     ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
  220.       /* Make sure components are not sent twice */
  221.       for (ci = 0; ci < ncomps; ci++) {
  222.     thisi = scanptr->component_index[ci];
  223.     if (component_sent[thisi])
  224.       ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
  225.     component_sent[thisi] = TRUE;
  226.       }
  227.     }
  228.   }
  229.  
  230.   /* Now verify that everything got sent. */
  231.   if (cinfo->progressive_mode) {
  232. #ifdef C_PROGRESSIVE_SUPPORTED
  233.     /* For progressive mode, we only check that at least some DC data
  234.      * got sent for each component; the spec does not require that all bits
  235.      * of all coefficients be transmitted.  Would it be wiser to enforce
  236.      * transmission of all coefficient bits??
  237.      */
  238.     for (ci = 0; ci < cinfo->num_components; ci++) {
  239.       if (last_bitpos[ci][0] < 0)
  240.     ERREXIT(cinfo, JERR_MISSING_DATA);
  241.     }
  242. #endif
  243.   } else {
  244.     for (ci = 0; ci < cinfo->num_components; ci++) {
  245.       if (! component_sent[ci])
  246.     ERREXIT(cinfo, JERR_MISSING_DATA);
  247.     }
  248.   }
  249. }
  250.  
  251. #endif /* C_MULTISCAN_FILES_SUPPORTED */
  252.  
  253.  
  254. LOCAL(void)
  255. select_scan_parameters (j_compress_ptr cinfo)
  256. /* Set up the scan parameters for the current scan */
  257. {
  258.   int ci;
  259.  
  260. #ifdef C_MULTISCAN_FILES_SUPPORTED
  261.   if (cinfo->scan_info != NULL) {
  262.     /* Prepare for current scan --- the script is already validated */
  263.     my_master_ptr master = (my_master_ptr) cinfo->master;
  264.     const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number;
  265.  
  266.     cinfo->comps_in_scan = scanptr->comps_in_scan;
  267.     for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
  268.       cinfo->cur_comp_info[ci] =
  269.     &cinfo->comp_info[scanptr->component_index[ci]];
  270.     }
  271.     cinfo->Ss = scanptr->Ss;
  272.     cinfo->Se = scanptr->Se;
  273.     cinfo->Ah = scanptr->Ah;
  274.     cinfo->Al = scanptr->Al;
  275.   }
  276.   else
  277. #endif
  278.   {
  279.     /* Prepare for single sequential-JPEG scan containing all components */
  280.     if (cinfo->num_components > MAX_COMPS_IN_SCAN)
  281.       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
  282.            MAX_COMPS_IN_SCAN);
  283.     cinfo->comps_in_scan = cinfo->num_components;
  284.     for (ci = 0; ci < cinfo->num_components; ci++) {
  285.       cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
  286.     }
  287.     cinfo->Ss = 0;
  288.     cinfo->Se = DCTSIZE2-1;
  289.     cinfo->Ah = 0;
  290.     cinfo->Al = 0;
  291.   }
  292. }
  293.  
  294.  
  295. LOCAL(void)
  296. per_scan_setup (j_compress_ptr cinfo)
  297. /* Do computations that are needed before processing a JPEG scan */
  298. /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
  299. {
  300.   int ci, mcublks, tmp;
  301.   jpeg_component_info *compptr;
  302.   
  303.   if (cinfo->comps_in_scan == 1) {
  304.     
  305.     /* Noninterleaved (single-component) scan */
  306.     compptr = cinfo->cur_comp_info[0];
  307.     
  308.     /* Overall image size in MCUs */
  309.     cinfo->MCUs_per_row = compptr->width_in_blocks;
  310.     cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
  311.     
  312.     /* For noninterleaved scan, always one block per MCU */
  313.     compptr->MCU_width = 1;
  314.     compptr->MCU_height = 1;
  315.     compptr->MCU_blocks = 1;
  316.     compptr->MCU_sample_width = DCTSIZE;
  317.     compptr->last_col_width = 1;
  318.     /* For noninterleaved scans, it is convenient to define last_row_height
  319.      * as the number of block rows present in the last iMCU row.
  320.      */
  321.     tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  322.     if (tmp == 0) tmp = compptr->v_samp_factor;
  323.     compptr->last_row_height = tmp;
  324.     
  325.     /* Prepare array describing MCU composition */
  326.     cinfo->blocks_in_MCU = 1;
  327.     cinfo->MCU_membership[0] = 0;
  328.     
  329.   } else {
  330.     
  331.     /* Interleaved (multi-component) scan */
  332.     if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
  333.       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
  334.            MAX_COMPS_IN_SCAN);
  335.     
  336.     /* Overall image size in MCUs */
  337.     cinfo->MCUs_per_row = (JDIMENSION)
  338.       jdiv_round_up((long) cinfo->image_width,
  339.             (long) (cinfo->max_h_samp_factor*DCTSIZE));
  340.     cinfo->MCU_rows_in_scan = (JDIMENSION)
  341.       jdiv_round_up((long) cinfo->image_height,
  342.             (long) (cinfo->max_v_samp_factor*DCTSIZE));
  343.     
  344.     cinfo->blocks_in_MCU = 0;
  345.     
  346.     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  347.       compptr = cinfo->cur_comp_info[ci];
  348.       /* Sampling factors give # of blocks of component in each MCU */
  349.       compptr->MCU_width = compptr->h_samp_factor;
  350.       compptr->MCU_height = compptr->v_samp_factor;
  351.       compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
  352.       compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;
  353.       /* Figure number of non-dummy blocks in last MCU column & row */
  354.       tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
  355.       if (tmp == 0) tmp = compptr->MCU_width;
  356.       compptr->last_col_width = tmp;
  357.       tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
  358.       if (tmp == 0) tmp = compptr->MCU_height;
  359.       compptr->last_row_height = tmp;
  360.       /* Prepare array describing MCU composition */
  361.       mcublks = compptr->MCU_blocks;
  362.       if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
  363.     ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
  364.       while (mcublks-- > 0) {
  365.     cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
  366.       }
  367.     }
  368.     
  369.   }
  370.  
  371.   /* Convert restart specified in rows to actual MCU count. */
  372.   /* Note that count must fit in 16 bits, so we provide limiting. */
  373.   if (cinfo->restart_in_rows > 0) {
  374.     long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
  375.     cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
  376.   }
  377. }
  378.  
  379.  
  380. /*
  381.  * Per-pass setup.
  382.  * This is called at the beginning of each pass.  We determine which modules
  383.  * will be active during this pass and give them appropriate start_pass calls.
  384.  * We also set is_last_pass to indicate whether any more passes will be
  385.  * required.
  386.  */
  387.  
  388. METHODDEF(void)
  389. prepare_for_pass (j_compress_ptr cinfo)
  390. {
  391.   my_master_ptr master = (my_master_ptr) cinfo->master;
  392.  
  393.   switch (master->pass_type) {
  394.   case main_pass:
  395.     /* Initial pass: will collect input data, and do either Huffman
  396.      * optimization or data output for the first scan.
  397.      */
  398.     select_scan_parameters(cinfo);
  399.     per_scan_setup(cinfo);
  400.     if (! cinfo->raw_data_in) {
  401.       (*cinfo->cconvert->start_pass) (cinfo);
  402.       (*cinfo->downsample->start_pass) (cinfo);
  403.       (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
  404.     }
  405.     (*cinfo->fdct->start_pass) (cinfo);
  406.     (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
  407.     (*cinfo->coef->start_pass) (cinfo,
  408.                 (master->total_passes > 1 ?
  409.                  JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
  410.     (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
  411.     if (cinfo->optimize_coding) {
  412.       /* No immediate data output; postpone writing frame/scan headers */
  413.       master->pub.call_pass_startup = FALSE;
  414.     } else {
  415.       /* Will write frame/scan headers at first jpeg_write_scanlines call */
  416.       master->pub.call_pass_startup = TRUE;
  417.     }
  418.     break;
  419. #ifdef ENTROPY_OPT_SUPPORTED
  420.   case huff_opt_pass:
  421.     /* Do Huffman optimization for a scan after the first one. */
  422.     select_scan_parameters(cinfo);
  423.     per_scan_setup(cinfo);
  424.     if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) {
  425.       (*cinfo->entropy->start_pass) (cinfo, TRUE);
  426.       (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
  427.       master->pub.call_pass_startup = FALSE;
  428.       break;
  429.     }
  430.     /* Special case: Huffman DC refinement scans need no Huffman table
  431.      * and therefore we can skip the optimization pass for them.
  432.      */
  433.     master->pass_type = output_pass;
  434.     master->pass_number++;
  435.     /*FALLTHROUGH*/
  436. #endif
  437.   case output_pass:
  438.     /* Do a data-output pass. */
  439.     /* We need not repeat per-scan setup if prior optimization pass did it. */
  440.     if (! cinfo->optimize_coding) {
  441.       select_scan_parameters(cinfo);
  442.       per_scan_setup(cinfo);
  443.     }
  444.     (*cinfo->entropy->start_pass) (cinfo, FALSE);
  445.     (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
  446.     /* We emit frame/scan headers now */
  447.     if (master->scan_number == 0)
  448.       (*cinfo->marker->write_frame_header) (cinfo);
  449.     (*cinfo->marker->write_scan_header) (cinfo);
  450.     master->pub.call_pass_startup = FALSE;
  451.     break;
  452.   default:
  453.     ERREXIT(cinfo, JERR_NOT_COMPILED);
  454.   }
  455.  
  456.   master->pub.is_last_pass = (master->pass_number == master->total_passes-1);
  457.  
  458.   /* Set up progress monitor's pass info if present */
  459.   if (cinfo->progress != NULL) {
  460.     cinfo->progress->completed_passes = master->pass_number;
  461.     cinfo->progress->total_passes = master->total_passes;
  462.   }
  463. }
  464.  
  465.  
  466. /*
  467.  * Special start-of-pass hook.
  468.  * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
  469.  * In single-pass processing, we need this hook because we don't want to
  470.  * write frame/scan headers during jpeg_start_compress; we want to let the
  471.  * application write COM markers etc. between jpeg_start_compress and the
  472.  * jpeg_write_scanlines loop.
  473.  * In multi-pass processing, this routine is not used.
  474.  */
  475.  
  476. METHODDEF(void)
  477. pass_startup (j_compress_ptr cinfo)
  478. {
  479.   cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
  480.  
  481.   (*cinfo->marker->write_frame_header) (cinfo);
  482.   (*cinfo->marker->write_scan_header) (cinfo);
  483. }
  484.  
  485.  
  486. /*
  487.  * Finish up at end of pass.
  488.  */
  489.  
  490. METHODDEF(void)
  491. finish_pass_master (j_compress_ptr cinfo)
  492. {
  493.   my_master_ptr master = (my_master_ptr) cinfo->master;
  494.  
  495.   /* The entropy coder always needs an end-of-pass call,
  496.    * either to analyze statistics or to flush its output buffer.
  497.    */
  498.   (*cinfo->entropy->finish_pass) (cinfo);
  499.  
  500.   /* Update state for next pass */
  501.   switch (master->pass_type) {
  502.   case main_pass:
  503.     /* next pass is either output of scan 0 (after optimization)
  504.      * or output of scan 1 (if no optimization).
  505.      */
  506.     master->pass_type = output_pass;
  507.     if (! cinfo->optimize_coding)
  508.       master->scan_number++;
  509.     break;
  510.   case huff_opt_pass:
  511.     /* next pass is always output of current scan */
  512.     master->pass_type = output_pass;
  513.     break;
  514.   case output_pass:
  515.     /* next pass is either optimization or output of next scan */
  516.     if (cinfo->optimize_coding)
  517.       master->pass_type = huff_opt_pass;
  518.     master->scan_number++;
  519.     break;
  520.   }
  521.  
  522.   master->pass_number++;
  523. }
  524.  
  525.  
  526. /*
  527.  * Initialize master compression control.
  528.  */
  529.  
  530. GLOBAL(void)
  531. jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
  532. {
  533.   my_master_ptr master;
  534.  
  535.   master = (my_master_ptr)
  536.       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  537.                   SIZEOF(my_comp_master));
  538.   cinfo->master = (struct jpeg_comp_master *) master;
  539.   master->pub.prepare_for_pass = prepare_for_pass;
  540.   master->pub.pass_startup = pass_startup;
  541.   master->pub.finish_pass = finish_pass_master;
  542.   master->pub.is_last_pass = FALSE;
  543.  
  544.   /* Validate parameters, determine derived values */
  545.   initial_setup(cinfo);
  546.  
  547.   if (cinfo->scan_info != NULL) {
  548. #ifdef C_MULTISCAN_FILES_SUPPORTED
  549.     validate_script(cinfo);
  550. #else
  551.     ERREXIT(cinfo, JERR_NOT_COMPILED);
  552. #endif
  553.   } else {
  554.     cinfo->progressive_mode = FALSE;
  555.     cinfo->num_scans = 1;
  556.   }
  557.  
  558.   if (cinfo->progressive_mode)    /*  TEMPORARY HACK ??? */
  559.     cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */
  560.  
  561.   /* Initialize my private state */
  562.   if (transcode_only) {
  563.     /* no main pass in transcoding */
  564.     if (cinfo->optimize_coding)
  565.       master->pass_type = huff_opt_pass;
  566.     else
  567.       master->pass_type = output_pass;
  568.   } else {
  569.     /* for normal compression, first pass is always this type: */
  570.     master->pass_type = main_pass;
  571.   }
  572.   master->scan_number = 0;
  573.   master->pass_number = 0;
  574.   if (cinfo->optimize_coding)
  575.     master->total_passes = cinfo->num_scans * 2;
  576.   else
  577.     master->total_passes = cinfo->num_scans;
  578. }
  579.